package cn.hx.controller;

import cn.hx.entity.Dept;
import cn.hx.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class DeptController {

    @Autowired
    DeptService deptService;

    @GetMapping("searchDept")
    public ModelAndView getSearchDept() {
        ModelAndView modelAndView = new ModelAndView("searchDept");
        modelAndView.addObject("dept",deptService.findAll());
        return modelAndView;
    }

    @GetMapping("createDept")
    public ModelAndView getCreateDept(){
        ModelAndView modelAndView = new ModelAndView("createDept");
        return modelAndView;
    }

//    修改回显
    @GetMapping("updateDept/{id}")
    public String getUpdateDept(@PathVariable Integer id,Model model){
        model.addAttribute("dept",deptService.queryById(id));
        return "updateDept";
    }
//    修改
    @PostMapping("editDept/{id}")
    public String getEditDept(@PathVariable Integer id, Dept dept){
        dept.setId(id);
        deptService.update(dept);
        return "redirect:/searchDept";
    }

//    删
    @GetMapping("deleteDept/{id}")
    public String getDeleteDept(@PathVariable Integer id){
        System.out.println(id);
        deptService.deleteById(id);
        return "redirect:/searchDept";
    }

//    增
    @PostMapping("saveDept")
    public String getSaveDept(Dept dept){
        dept.setNumber(String.valueOf(System.currentTimeMillis()));
        deptService.insert(dept);
        return "redirect:/searchDept";
    }

}
